import { defineStore } from 'pinia'

export interface CheckerType{
    whiteWeights:number; //白棋权重
    blackWeights:number;//黑棋权重
    color:'white'|'black'|'unused';//棋子颜色
    pointId:string;//对应浏览器点上的id
    xPos:number;
    yPos:number;
    isUsed:boolean;
    isListened:boolean;
    isDone:boolean;
    north:CheckerType|null;
    south:CheckerType|null;
    northwest:CheckerType|null;
    northeast:CheckerType|null;
    southwest:CheckerType|null;
    southeast:CheckerType|null;
}

interface IInitialState{
    rowNum:number;
    colNum:number;
    checkers:CheckerType[][];
    currentColor:'white'|'black';
    isWon:'white'|'black'|null;
    isBan:boolean,
    ws:WebSocket | undefined,
    connected:boolean,
    pingTime:number,
    reconnectTime:number,
    prePosition:number[]
}

const useCheckerStore = defineStore('checker',{
    state:()=>{
        const initialState:IInitialState = {
            rowNum: 29,
            colNum: 15,
            checkers: [],
            currentColor: 'white',
            isWon:null,
            isBan:false,
            ws:undefined,
            connected:false,
            pingTime:10000,
            reconnectTime:20000,
            prePosition:[-1,-1]
        }
        for (let i = 0; i < initialState.rowNum; i++) {
            let row:CheckerType[] = []
            for (let j = 0; j < initialState.colNum; j++) {
                let item:CheckerType = {
                    whiteWeights: 0,
                    blackWeights:0,
                    color: 'unused',
                    isUsed: false,
                    isListened:false,
                    isDone:false,
                    pointId: 'box' + '-' + i + '-' + j,
                    north: null,
                    south: null,
                    northwest: null,
                    northeast: null,
                    southwest: null,
                    southeast: null,
                    xPos: i,
                    yPos: j
                }
                row.push(item);
            }
            initialState.checkers.push(row);
        }
        return initialState;
    },
    actions:{
        judgeWin(checker: CheckerType): boolean{
            //分别向6个方向进行查询
            const color = checker.color;
            let node: CheckerType | null = checker;
            let cnt = 1;
            //向北查找
            while (true) {
                node = node?.north;
                if (node === null || node.color !== color) {
                    break;
                }
                cnt++;
            }
            node = checker;
            //向南查找
            while (true) {
                node = node?.south;
                if (node === null || node.color !== color) {
                    break;
                }
                cnt++;
            }
            if (cnt >= 5) {
                return true;
            }
        
            cnt = 1;
            node = checker;
            //向西北查找
            while (true) {
                node = node?.northwest;
                if (node === null || node.color !== color) {
                    break;
                }
                cnt++;
            }
            node = checker;
            //向东南查找
            while (true) {
                node = node?.southeast;
                if (node === null || node.color !== color) {
                    break;
                }
                cnt++;
            }
        
            if (cnt >= 5) {
                return true;
            }
        
            cnt = 1;
            node = checker;
            //向东北查找
            while (true) {
                node = node?.northeast;
                if (node === null || node.color !== color) {
                    break;
                }
                cnt++;
            }
            node = checker;
            //向西南查找
            while (true) {
                node = node?.southwest;
                if (node === null || node.color !== color) {
                    break;
                }
                cnt++;
            }
            if (cnt >= 5) {
                return true;
            }
            return false;
        },
        dropPiece(x: number, y: number){
            const checker = this.checkers[x][y];
            if (checker.isUsed) {
                return;
            }
            this.prePosition = [x,y]
            checker.isUsed = true;
            checker.color = this.currentColor;
            this.currentColor = this.currentColor === 'white' ? 'black' : 'white';
            if (this.judgeWin(checker)) {
                this.isWon = checker.color;
                // removeListener()
            }
            this.countWeights()
        },
        countWeights(){
            for (let i = 0; i < this.rowNum; i++) {
                for (let j = 0; j < this.colNum; j++) {
                    if (!this.checkers[i][j].isDone || this.checkers[i][j].isUsed) {
                        continue;
                    }
                    this.countWeight(this.checkers[i][j], 'black');
                    this.countWeight(this.checkers[i][j], 'white');
                }
        
            }
        },
        countWeight(checker: CheckerType, color: 'white' | 'black'){
            const antiColor = color === 'white' ? 'black' : 'white';
            let node: CheckerType | null = checker;
            let cnt1 = 0;
            let space1 = 1;
            //向北查找
            let patience = 1;
            while (true) {
                node = node?.north;
                console.log(node?.color);
                
                if (node === null) {
                    break;
                }else if(node.color === antiColor){
                    cnt1 -= 1;
                    break;
                } else if(node.color === color){
                    if (patience > 0) {
                        cnt1 += 10;
                    }
                }else if(node.color === 'unused'){
                    patience -= 1;
                }
                space1 += 1;
            }
            node = checker;
            //向南查找
            patience = 1;
            while (true) {
                node = node?.south;
                if (node === null) {
                    break;
                }else if (node.color === antiColor) {
                    cnt1 -= 1;
                    break;
                }else if(node.color === color) {
                    if (patience > 0) {
                        cnt1 += 10;
                    }
                }else if (node.color === 'unused') {
                    patience -= 1;
                }
                space1 += 1;
            }
            if (space1 < 5) {
                cnt1 = 0;
            }
            let cnt2 = 0;
            let space2 = 1;
            node = checker;
            //向西北查找
            patience = 1;
            while (true) {
                node = node?.northwest;
                if (node === null) {
                    break;
                }else if (node.color === antiColor) {
                    cnt2 -= 1;
                    break;
                }else if (node.color === color) {
                    if (patience > 0) {
                        cnt2 += 10;
                    }
                }else if (node.color === 'unused') {
                    patience -= 1;
                }
                space2 += 1;
            }
            node = checker;
            //向东南查找
            patience = 1;
            while (true) {
                node = node?.southeast;
                if (node === null) {
                    break;
                }else if (node.color === antiColor) {
                    cnt2 -= 1;
                    break;
                }else if(node.color === color){
                    if (patience > 0) {
                        cnt2 += 10;
                    }
                }else if (node.color === 'unused') {
                    patience -= 1;
                }
                space2 += 1;
            }
            if (space2 < 5) {
                cnt2 = 0;
            }
            let cnt3 = 0;
            let space3 = 1;
            node = checker;
            //向东北查找
            patience = 1;
            while (true) {
                node = node?.northeast;
                if (node === null) {
                    break;
                }else if (node.color === antiColor) {
                    cnt3 -= 1;
                    break;
                }else if(node.color === color){
                    if (patience > 0) {
                        cnt3 += 10;
                    }
                }else if (node.color === 'unused') {
                    patience -= 1;
                }
                space3 += 1;
            }
            node = checker;
            //向西南查找
            patience = 1;
            while (true) {
                node = node?.southwest;
                if (node === null) {
                    break
                }else if (node.color === antiColor) {
                    cnt3 -= 1;
                    break;
                }else if (node.color === color) {
                    if (patience > 0) {
                        cnt3 += 10;
                    }
                }else if (node.color === 'unused') {
                    patience -= 1;
                }
            }
            if (space3 < 5) {
                cnt3 = 0;
            }
        
            if (color === 'white') {
                checker.whiteWeights = Math.max(cnt1, cnt2, cnt3);
            } else {
                checker.blackWeights = Math.max(cnt1, cnt2, cnt3);
            }
        },
        getPointPos(){
            let x: number, y: number;
            let x1 = 0, y1 = 0, x2 = 0, y2 = 0;
            let whiteMax = -1, blackMax = -1;
            for (let i = 0; i < this.rowNum; i++) {
                for (let j = 0; j < this.colNum; j++) {
                    if (!this.checkers[i][j].isDone || this.checkers[i][j].isUsed) {
                        continue;
                    }
                    if (whiteMax < this.checkers[i][j].whiteWeights) {
                        whiteMax = this.checkers[i][j].whiteWeights;
                        x1 = i;
                        y1 = j;
                    }
                    if (blackMax < this.checkers[i][j].blackWeights) {
                        blackMax = this.checkers[i][j].blackWeights;
                        x2 = i;
                        y2 = j;
                    }
                }
        
            }
            if (whiteMax > blackMax) {
                x = x1;
                y = y1;
            } else {
                x = x2;
                y = y2;
            }
            return [x, y];
        }
    }
})

export default useCheckerStore;